﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cdhpy.Util
{
    public sealed class Format
    {
        /// <summary>
        /// 把数组所有元素，依字母的ASCII顺序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
        /// </summary>
        /// <param name="paraMap"></param>
        /// <param name="urlencode"></param>
        /// <returns></returns>
        public static string QueryParaMap(Dictionary<string, string> paraMap)
        {

            string buff = "";
            try
            {
                var result = from pair in paraMap orderby pair.Key select pair;
                foreach (KeyValuePair<string, string> pair in result)
                {
                    if (pair.Key != "")
                    {

                        string key = pair.Key;
                        string val = pair.Value;
                        buff += key.ToLower() + "=" + val + "&";

                    }
                }

                if (buff.Length == 0 == false)
                {
                    buff = buff.Substring(0, (buff.Length - 1) - (0));
                }
            }
            catch
            {
                throw;
            }
            return buff;
        }

        /// <summary>
        /// 把数组所有元素，依字母的ASCII顺序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
        /// </summary>
        /// <param name="paraMap"></param>
        /// <param name="urlencode"></param>
        /// <returns></returns>
        public static string QueryParaMap(SortedDictionary<string, string> paraMap)
        {

            string buff = "";
            try
            {
                foreach (KeyValuePair<string, string> pair in paraMap)
                {
                    if (pair.Key != "")
                    {

                        string key = pair.Key;
                        string val = pair.Value;
                        buff += key.ToLower() + "=" + val + "&";

                    }
                }

                if (buff.Length == 0 == false)
                {
                    buff = buff.Substring(0, (buff.Length - 1) - (0));
                }
            }
            catch
            {
                throw;
            }
            return buff;
        }

        /// <summary>
        /// 将数字压缩成字符串（基于数字的进制转换）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="baseChars"></param>
        /// <returns></returns>
        public static string IntToString(long value, char[] baseChars)
        {
            string result = string.Empty;
            int targetBase = baseChars.Length;

            do
            {
                result = baseChars[value % targetBase] + result;
                value = value / targetBase;
            }
            while (value > 0);

            return result;
        }

        /// <summary>
        /// 将数字压缩成字符串（基于数字的进制转换）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="baseChars"></param>
        /// <returns></returns>
        public static string IntToString(long value)
        {
            char[] baseChars = { '0','1','2','3','4','5','6','7','8','9',
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x',
            '!','~','*','$','-','_','.',',','?','#','[',']',';',':','`','+','=','|','{','}','(',')','<','>'};
            return IntToString(value, baseChars);
        }

        /// <summary>
        /// 转换成枚举值
        /// </summary>
        /// <returns></returns>
        public static T ParseEnum<T>(string value, T defaultVal)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), value, true);
            }
            catch
            {
                return defaultVal;
            }
        }

        /// <summary>
        /// 两数相除获取百分比
        /// </summary>
        /// <param name="upNum">分子</param>
        /// <param name="downNum">分母</param>
        /// <returns>返回 “xx.xx%” 格式数据。 不过如果小数点最后为0，会省略0</returns>
        public static string GetPercentageWithNumberDevided(int upNum, int downNum)
        {
            if (upNum == 0 || downNum == 0)
                return "0%";

            decimal result = Decimal.Divide(upNum, downNum);
            return GetPercentageWithNumberDevided(result);
        }

        /// <summary>
        /// 两数相除获取百分比
        /// </summary>
        /// <param name="result">计算结果</param>
        /// <returns>返回 “xx.xx%” 格式数据。 不过如果小数点最后为0，会省略0</returns>
        public static string GetPercentageWithNumberDevided(decimal result)
        {
            if (result == 0)
                return "0%";

            string percent = String.Format("{0:P2}", result);
            int percentStrLength = percent.Length;
            StringBuilder sb = new StringBuilder(percent);
            if (sb[percentStrLength - 2] == '0')
            {
                sb.Remove(percentStrLength - 2, 1);
                if (sb[percentStrLength - 3] == '0')
                {
                    sb.Remove(percentStrLength - 4, 2);
                }
            }
            return sb.ToString();
        }
    }
}
